package com.springboot.hiresystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.hiresystem.domain.Applications;
import com.springboot.hiresystem.domain.Resumes;
import com.springboot.hiresystem.dto.req.ApplicationQueryRequestDTO;
import com.springboot.hiresystem.dto.req.ApplicationUpdateRequestDTO;
import com.springboot.hiresystem.dto.resp.ApplicationResponseDTO;
import com.springboot.hiresystem.dto.resp.PageResponse;
import com.springboot.hiresystem.dto.resp.Result;
import com.springboot.hiresystem.service.ApplicationsService;
import com.springboot.hiresystem.service.ResumesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/applications")
@Slf4j
public class ApplicationController {

    private final ApplicationsService applicationService;
    private final ResumesService resumesService;

    public ApplicationController(ApplicationsService applicationService, ResumesService resumesService) {
        this.applicationService = applicationService;
        this.resumesService = resumesService;
    }

    // 分页查询
    @GetMapping("/list")
    public Result<PageResponse<ApplicationResponseDTO>> list(ApplicationQueryRequestDTO request) {
        QueryWrapper<Applications> queryWrapper = new QueryWrapper<>();
        if (request.getUserId() != null) {
            queryWrapper.eq("user_id", request.getUserId());
        }
        if (request.getJobId() != null) {
            queryWrapper.eq("job_id", request.getJobId());
        }
        queryWrapper.orderByAsc("applied_at");

        Page<Applications> page = new Page<>(request.getPage(), request.getSize());
        Page<Applications> resultPage = applicationService.page(page, queryWrapper);

        List<ApplicationResponseDTO> dtoList = BeanUtil.copyToList(resultPage.getRecords(), ApplicationResponseDTO.class);

        PageResponse<ApplicationResponseDTO> response = new PageResponse<>();
        response.setTotal(resultPage.getTotal());
        response.setList(dtoList);

        return Result.success(response);
    }

    // 条件查询
    @PostMapping("/query")
    public Result<List<ApplicationResponseDTO>> query(@RequestBody ApplicationQueryRequestDTO request) {
        QueryWrapper<Applications> queryWrapper = new QueryWrapper<>();

        if (request.getUserId() != null) {
            queryWrapper.eq("user_id", request.getUserId());
        }
        if (request.getJobId() != null) {
            queryWrapper.eq("job_id", request.getJobId());
        }

        List<Applications> applications = applicationService.list(queryWrapper);
        List<ApplicationResponseDTO> dtoList = BeanUtil.copyToList(applications, ApplicationResponseDTO.class);

        return Result.success(dtoList);
    }

    // 更新或添加
    @PostMapping("/update")
    public Result<String> update(@RequestBody ApplicationUpdateRequestDTO request) {
        Applications application = BeanUtil.copyProperties(request, Applications.class);
        if (application.getApplicationId() != null && application.getApplicationId() > 0) {
            applicationService.updateById(application);
            return Result.success("更新成功");
        } else {
            if (!applicationService.save(application)) {
                return Result.failure(500, "添加失败");
            }
            return Result.success("新增成功");
        }
    }

    // 删除
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody Applications application) {
        applicationService.removeById(application.getApplicationId());
        return Result.success("删除成功");
    }
    //用户申请
    @PostMapping("/userApply")
    public Result<String> add(@RequestBody Applications app) {
        if(app.getUserId()==null||app.getJobId()==null){return Result.failure(500,"信息不全");}else {
        QueryWrapper<Resumes> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", app.getUserId());
            queryWrapper.eq("is_default", 1);

            List<Resumes> resumes = resumesService.list(queryWrapper);

        Applications application = new Applications();
        application.setUserId(app.getUserId());
        application.setJobId(app.getJobId());
        application.setResumeId(resumes.get(0).getResumeId());
        //如果这条信息已存在,则不保存
        if (applicationService.getOne(new QueryWrapper<Applications>().eq("user_id", app.getUserId()).eq("job_id", app.getJobId())) != null) {
            return Result.failure(500, "您已经申请过该职位");
        }
            applicationService.save(application);
        System.out.println("---------------------------");
        System.out.println(application);
        return Result.success("投递成功");
    }
    }

}
